// pages/allclass/index.js
import urls from '../../utils/urls';
import loginFn from '../../utils/login.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		getPhoneNumber: false, //是否已经获取手机号码
		tabs: ["待审核", "已通过", "未通过", "统计"], //tab选项内容
		activeIndex: 1, //tab当前选中
		schoolStatus: 3, //审核通过
		list: [],
		statusList: [] //统计列表
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var phoneNum = wx.getStorageSync('phone')
		if (phoneNum) {
			this.setData({
				getPhoneNumber: true
			})
		} else {
			this.setData({
				getPhoneNumber: false
			})
		};
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this;
		var loginInfo = loginFn()
		wx.request({
			url: urls.schoolList,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var status_a = 0
					var status_b = 0
					var status_c = 0
					for (var k = 0; k < res.data.data.length; k++) {
						if (res.data.data[k].status == 0) {
							status_a += 1
						} else if (res.data.data[k].status == 3) {
							status_b += 1
						} else if (res.data.data[k].status == 4) {
							status_c += 1
						}
					}
					if (status_a > 0) {
						that.data.tabs[0] = '待审核' + '（' + status_a + '）';
					} else {
						that.data.tabs[0] = '待审核';
					};
					if (status_b > 0) {
						that.data.tabs[1] = '已通过' + '（' + status_b + '）';
					} else {
						that.data.tabs[1] = '已通过'
					};
					if (status_c > 0) {
						that.data.tabs[2] = '未通过' + '（' + status_c + '）';
					} else {
						that.data.tabs[2] = '未通过'
					};
					that.setData({
						list: res.data.data,
						tabs: that.data.tabs
					})
				};
			}
		})
		//统计列表
		this.getstalist(loginInfo)
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	tabClick: function(e) { //点击tab
		var schoolStatus = 3;
		if (e.currentTarget.id == 0) {
			schoolStatus = 0;
		} else if (e.currentTarget.id == 1) {
			schoolStatus = 3;
		} else if (e.currentTarget.id == 2) {
			schoolStatus = 4;
		}
		this.setData({
			activeIndex: e.currentTarget.id,
			schoolStatus: schoolStatus
		});
	},
	getstalist: function(loginInfo) { //统计列表
		var that = this;
		wx.request({
			url: urls.statistics,
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0' && res.data.data.length > 0) {
					that.setData({
						statusList: res.data.data
					})
				};
			}
		})
	},
	gotosta: function(e) {
		wx.navigateTo({
			url: '/pages/statistics/index?month=' + e.currentTarget.dataset.month,
		})
	},
	addinsFn: function(e) { //点击添加机构
		console.log(e)
		wx.navigateTo({
			url: '/pages/institutions/institutions',
		})
	},

	editinstFn: function(e) { //修改机构
		console.log(e)
		wx.navigateTo({
			url: '/pages/addindex/index?id=' + e.currentTarget.dataset.id,
		})
	}
})
